
package com.example.sunran.ui;
import com.example.sunran.service.FTPService;
import com.example.sunran.service.impl.FTPServiceImpl;
import org.apache.commons.net.ftp.FTPFile;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

public class IndexUI {

	private JFrame frmFtp;
	private JTextField hostTextField;
	private JTextField userTextField;
	private JTextField pwdTextField;
	private JTextField portTextField;
	private JTextField localPathTextField;
	private JTextField ftpPathTextField;
	private JList localFileList;
	private JList ftpFileList;
	private JTextArea logTextArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IndexUI window = new IndexUI();
					window.frmFtp.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public IndexUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		MyClickListener clickListener = new MyClickListener();
		
		frmFtp = new JFrame();
		frmFtp.setTitle("FTP\u6587\u4EF6\u4F20\u8F93");
		frmFtp.setSize(820,600);
		frmFtp.setResizable(false);
		frmFtp.setLocationRelativeTo(null);
		frmFtp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmFtp.getContentPane().setLayout(null);
		
		hostTextField = new JTextField();
		hostTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		hostTextField.setBounds(73, 10, 147, 26);
		frmFtp.getContentPane().add(hostTextField);
		hostTextField.setText("127.0.0.1");
		hostTextField.setColumns(10);
		
		userTextField = new JTextField();
		userTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		userTextField.setColumns(10);
		userTextField.setBounds(306, 10, 101, 26);
		userTextField.setText("ftp");
		frmFtp.getContentPane().add(userTextField);
		
		pwdTextField = new JTextField();
		pwdTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		pwdTextField.setColumns(10);
		pwdTextField.setBounds(477, 10, 101, 26);
		pwdTextField.setText("ftp");
		frmFtp.getContentPane().add(pwdTextField);
		
		portTextField = new JTextField();
		portTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		portTextField.setColumns(10);
		portTextField.setBounds(644, 10, 32, 26);
		portTextField.setText("21");
		frmFtp.getContentPane().add(portTextField);
		
		JLabel lblNewLabel = new JLabel("\u4E3B\u673Aip");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(10, 15, 58, 15);
		frmFtp.getContentPane().add(lblNewLabel);
		
		JLabel label = new JLabel("\u7528\u6237\u540D");
		
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(243, 15, 58, 15);
		frmFtp.getContentPane().add(label);
		label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		JLabel label_1 = new JLabel("\u5BC6\u7801");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setBounds(417, 15, 58, 15);
		frmFtp.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("\u7AEF\u53E3");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setBounds(588, 15, 58, 15);
		frmFtp.getContentPane().add(label_2);
		
		JButton connectButton = new JButton("\u8FDE\u63A5");
		connectButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		connectButton.setBounds(712, 11, 75, 23);
		connectButton.addActionListener(clickListener);
		frmFtp.getContentPane().add(connectButton);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(238, 232, 170));
		panel.setBounds(0, 48, 816, 516);
		frmFtp.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("\u672C\u5730\u670D\u52A1\u5668");
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_1.setBounds(36, 28, 72, 37);
		panel.add(lblNewLabel_1);
		
		JLabel lblFtp = new JLabel("FTP\u670D\u52A1\u5668");
		lblFtp.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblFtp.setHorizontalAlignment(SwingConstants.CENTER);
		lblFtp.setBounds(483, 28, 82, 37);
		panel.add(lblFtp);

		localFileList = new JList();
		localFileList.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		localFileList.setBounds(36, 75, 258, 349);
		localFileList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {

				if (localFileList.getSelectedValue() == null) {
					return;
				}
				localPathTextField.setText(localFileList.getSelectedValue().toString());
			}
		});
		panel.add(localFileList);

		ftpFileList = new JList();
		ftpFileList.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		ftpFileList.setBounds(496, 75, 258, 349);
		ftpFileList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (ftpFileList.getSelectedValue() == null) {
					return;
				}
				ftpPathTextField.setText(ftpFileList.getSelectedValue().toString());
			}
		});
		panel.add(ftpFileList);
		
		localPathTextField = new JTextField();
		localPathTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		localPathTextField.setBounds(113, 36, 181, 21);
		panel.add(localPathTextField);
		localPathTextField.setColumns(10);
		
		ftpPathTextField = new JTextField();
		ftpPathTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		ftpPathTextField.setColumns(10);
		ftpPathTextField.setBounds(563, 36, 181, 21);
		panel.add(ftpPathTextField);
		
		JButton uploadButton = new JButton("\u4E0A\u4F20");
		uploadButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		uploadButton.setBounds(350, 153, 97, 23);
		uploadButton.addActionListener(clickListener);
		panel.add(uploadButton);
		
		JButton downloadButton = new JButton("\u4E0B\u8F7D");
		downloadButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		downloadButton.setBounds(350, 256, 97, 23);
		downloadButton.addActionListener(clickListener);
		panel.add(downloadButton);
		
		JLabel lblNewLabel_2 = new JLabel("\u300B\u300B\u300B");
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setBounds(446, 157, 52, 15);
		panel.add(lblNewLabel_2);
		
		JLabel label_3 = new JLabel("\u300A\u300A\u300A");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setBounds(293, 260, 57, 15);
		panel.add(label_3);
		
		JLabel label_4 = new JLabel("\u5386\u53F2\u6D88\u606F");
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		label_4.setBounds(367, 409, 58, 15);
		panel.add(label_4);

		logTextArea = new JTextArea();
		logTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		logTextArea.setBounds(36, 434, 718, 72);
	
		
	
		
		JScrollPane scrollPane = new JScrollPane(logTextArea);
		scrollPane.setBounds(36, 434, 718, 72);
		panel.add(scrollPane);
		
		
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(175, 238, 238));
		panel_1.setBounds(0, 0, 816, 47);
		frmFtp.getContentPane().add(panel_1);
	}

	class MyClickListener implements ActionListener{

		private static final String LocalPath = "d://LocalFile";
		private static final String FTPPath = "d://FTPFile";

		FTPService ftpService = new FTPServiceImpl();

		public void actionPerformed(ActionEvent e) {
			
			switch (e.getActionCommand()){
				case "连接":
					String host = hostTextField.getText();
					String user = userTextField.getText();
					String pwd = pwdTextField.getText();
					int port = Integer.valueOf(portTextField.getText());
					boolean res = ftpService.initFtp(host, user, pwd, port);

					if(res){
						JOptionPane.showMessageDialog(frmFtp, "登录成功");
						List<File> fileList = ftpService.getLocalFile(LocalPath);
						localFileList.setListData(fileList.toArray());

						List<String> ftpFiles = ftpService.getFtpFile();
						ftpFileList.setListData(ftpFiles.toArray());

						logTextArea.append("用户 "+user+" ->->-> 登录成功\n");
					}else{
						JOptionPane.showMessageDialog(frmFtp, "登录失败");

						logTextArea.append("用户 "+user+" ->->-> 登录失败\n");
					}
					break;
				case "下载":
					final String filePath1 = ftpFileList.getSelectedValue().toString();

					new Thread(new Runnable() {
						@Override
						public void run() {
							logTextArea.append("正在下载文件 ->->-> "+filePath1+"\n");
							ftpService.downloadFile("/", filePath1, LocalPath);
							updateFileList();
							logTextArea.append(filePath1+ " ->->->下载成功\n");
						}
					}).start();
					break;
				case "上传":
					String filePath2 = localFileList.getSelectedValue().toString();
					final File file2 = new File(filePath2);
					if (!file2.exists()) {
						break;
					}
					new Thread(new Runnable() {
					@Override
					public void run() {
						logTextArea.append("正在上传文件 ->->->"+ file2.getName()+"\n");
						ftpService.uploadFile("/", file2.getName(), file2);
						updateFileList();
						logTextArea.append(file2.getName()+" ->->-> 上传成功\n");
					}
				}).start();
				break;
			}

			
		}

		public void updateFileList(){
			List<File> fileList = ftpService.getLocalFile(LocalPath);
			localFileList.setListData(fileList.toArray());

			List<String> ftpFiles = ftpService.getFtpFile();
			ftpFileList.setListData(ftpFiles.toArray());
		}
	}



}
